<?php
use oxide\plugin\PluginAbstract;
use oxide\http\Context;

class AdminPlusPlugin extends PluginAbstract
{
   public function plug(Context $context) 
   {
      $this->register('AdminContentPostForm');
      $this->register('ContentDefaultPostStart');
      $this->register('ContentDefaultPostEnd');
      $this->register('FrontControllerPostRoute');
      $this->register('CommandControllerExecute');
   }
   
   public function onAdminContentPostForm($event, $sender, $args)
   {
      $form = $args['form'];
      $content = $args['content'];
      
      // we will move some information to different tab
      $parentControl = $form->removeControl('parent_pk');
      $permalinkControl = $form->removeControl('permalink');
      $modifiedControl = $form->removeControl('modified_on');
      $createdControl = $form->removeControl('created_on');
      
      $miscFieldset = new oxide\ui\html\Fieldset('misc', null, 'Misc.');
      $miscFieldset->addControls(array(
          $form->removeControl('type_pk'),
          $parentControl, $permalinkControl,$modifiedControl, $createdControl
          ));
      $form->addControl($miscFieldset);
      
      // this is require
      if($content->content == '') {
         $contentControl = $form->findControl('content');
         if($contentControl->setValue('n/a'));
      }
   }
   
   public function onCommandControllerExecute($event, \oxide\http\CommandController $sender = null, $args = null)
   {
      if($sender->getModuleName() == 'admin') {
         _html::link('/plugin/AdminPlus/resource/admin.css');
         $kcfinder_dir = $this->getConfigValue('kcfinder_dir');
         $browse_link = "{$kcfinder_dir}/browse.php?type=images";
         
         // add new item for image browser
         $span = new oxide\ui\html\Element('a', 
            new oxide\ui\html\Element('img', null, array('src' => '/plugin/AdminPlus/resource/browse-icon.png')), 
                 array('href' => $browse_link, 'target' => '_blank' ));
         $span->append('<br/>');
         $span->append('Image Browser');
         _template::navigations($span->render());
         
         
         _html::script('/plugin/AdminPlus/resource/load.js');
         _html::script('/plugin/AdminPlus/resource/tabs.js');
         
      }
      
      

   }
   
   public function onFrontControllerPostRoute($event, $sender = null, $args = null)
   {
      $route = $args['route'];
      if($route->module == 'plugin' && 
         $route->controller == 'AdminPlus' &&
         $route->action == 'upload') {
         print_r($_FILES);
      }
   }
   
   public function onContentDefaultPostStart($event, $sender, $args = null)
   {
      oxide\util\Loader::loadHelper('_html');
            
      $kcfinder_dir = $this->getConfigValue('kcfinder_dir');
      $autosave_interval = $this->getConfigValue('autosave_interval');
      $rich_editor = $this->getConfigValue('rich_editor', 'ckeditor');
      
      
      // adding autosave feature if supported
      if($autosave_interval) {
         _html::script(null, "plugin_adminplus_autosave_interval = {$autosave_interval}");
         _html::script('/plugin/AdminPlus/resource/autosave.js');
      }
      
      if($rich_editor == 'tinymce') {
         _html::script('//tinymce.cachefly.net/4.0/tinymce.min.js');
//         _html::script('/plugin/AdminPlus/resource/tinymce/js/tinymce/jquery.tinymce.min.js');
//         _html::script('/plugin/AdminPlus/resource/tinymce/js/tinymce/tinymce.min.js');
         
         $script = <<< EOF
tinymce.init({
    selector: "textarea[name=content]",
    plugins: [
         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
         "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
         "save table contextmenu directionality emoticons template paste textcolor"
   ]
                 
                 
});
EOF;
         _html::script(null, $script);         
         
      } else {
         $script = <<< EOF
$(document).ready(function(){
   CKEDITOR.replace( 'content', {
      filebrowserImageUploadUrl :   "{$kcfinder_dir}/upload.php?type=images",
      filebrowserUploadUrl :        '{$kcfinder_dir}/upload.php?type=files',
      filebrowserImageBrowseUrl :   '{$kcfinder_dir}/browse.php?type=images',
      filebrowserBrowseUrl :        '{$kcfinder_dir}/browse.php?type=files'
   });
});
EOF;
         _html::script(null, $script);
         
         _html::script('/plugin/AdminPlus/resource/ckeditor/ckeditor.js');
      }

      
      // auto complete for tabs
      $tagsObjects = \module\content\model\Tag::findAll();
      $tags = array();
      foreach ($tagsObjects as $tag) {
         $obj = new stdClass();
         $obj->value = $tag->term;
         $obj->label = $tag->title;
         $tags[] = $obj;
      }
      
      $js_array = json_encode($tags);
      $autocomplete = <<<EOF
              
    var availableTags = $js_array;
EOF;
      _html::script(null, $autocomplete);
      _html::script('/plugin/AdminPlus/resource/autocomplete.js');

   }
   
   
   public function onContentDefaultPostEnd($event, $sender, $args = null)
   {
   }
}
?>
